package framework.utils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;


/**
 * 汉字拼音工具类
 * @ClassName: PinYinUtil
 * @Description: 
 * @author li.quan 
 * @date 2016-6-29 下午02:48:35
 * @version V1.0
 */
public class PinYinUtil {
	/**
	 * 多音字姓氏集合
	 */
	public static final Map<String,String> MULTIPLE_FIRSTNAME =createMultipleFirstnameMap() ;
 
	
    private static Map<String, String> createMultipleFirstnameMap() {
        Map<String, String> result = new HashMap<String, String>();
        result.put("柏", "bai");
        result.put("贲", "ben");
        result.put("薄", "bo");
        result.put("都", "du");
        result.put("颉", "jie");
        result.put("解", "xie");
        result.put("秘", "bi");
        result.put("长", "chang");
        result.put("长孙", "zhangsun");
        result.put("牟", "mou");
        result.put("莘", "shen");
        result.put("殷", "yin");
        result.put("隽", "juan");
        result.put("尉", "wei");
        result.put("尉迟", "yu");
        result.put("奇", "ji");
        result.put("宓", "mi");
        result.put("盖", "ge");
        result.put("覃", "qin");
        result.put("单", "shan");
        result.put("单于", "chan");
        result.put("谌", "shen");
        result.put("翟", "zhai");
        result.put("乐", "yue");
        result.put("召", "shao");
        result.put("隗", "kui");
        result.put("种", "chong");
        result.put("朴", "piao");
        result.put("仇", "qiu");
        result.put("区", "ou");
        result.put("折", "she");
        result.put("黑", "hei");
        result.put("繁", "po");
        result.put("纪", "ji");
        result.put("查", "cha");
        result.put("郇", "huan");
        result.put("弗", "fu");
        result.put("褚", "chu");
        result.put("适", "shi");
        result.put("句", "ju");
        result.put("阚", "kan");
        result.put("乜", "niè");
        result.put("眭", "sui");
        result.put("洗", "xian");
        result.put("员", "yun");
        result.put("祭", "zhai");
        result.put("宿", "su");
        result.put("缪", "miao");
        result.put("乘", "cheng");
        result.put("辟", "bi");
        result.put("车", "che");
        result.put("会", "kuai");
        result.put("铅", "qian");
        result.put("茄", "qie");
        result.put("万", "wan");
        result.put("万俟", "moqi");
        result.put("吾", "wu");
        result.put("曾", "zeng");
       
        return Collections.unmodifiableMap(result);
    }
	
	/**
	 * 将字符串转换成拼音数组
	 * 
	 * @param str
	 *            待转换的汉字字符串
	 * @param onlyFirst
	 *            是否只取每个汉字对应的拼音的首字母
	 * @return 转换成拼音码的字符串
	 */
	public static String stringToPinyin(String str, boolean onlyFirst) {
		return stringToPinyin(str, onlyFirst, false, null);
	}

	/**
	 * 将字符串转换成拼音数组
	 * 
	 * @param str
	 *            待转换的汉字字符串
	 * @param onlyFirst
	 *            是否只取每个汉字对应的拼音的首字母
	 * @return 转换成拼音码的字符串
	 */
	public static String stringToPinyin(String str, boolean onlyFirst, String separator) {
		return stringToPinyin(str, onlyFirst, true, separator);
	}

	/**
	 * 将字符串转换成拼音数组
	 * 
	 * @param str
	 *            待转换的汉字字符串
	 * @param isPolyphone
	 *            是否查出多音字的所有拼音
	 * @param separator
	 *            多音字拼音之间的分隔符
	 * @return
	 */
	public static String stringToPinyin(String str, boolean onlyFirst, boolean isPolyphone, String separator) {
		// 判断字符串是否为空
		if ("".equals(str) || null == str) {
			return null;
		}
		char[] strChar = str.toCharArray();
		int strCount = strChar.length;
		StringBuffer sb = new StringBuffer();

		String temp = null;
		for (int i = 0; i < strCount; i++) {
			temp = charToPinyin(strChar[i], isPolyphone, separator);
			if (onlyFirst) {
				sb.append(temp.substring(0, 1));
			} else {
				sb.append(temp);
			}
		}
		return sb.toString();
	}

	/**
	 * 将单个字符转换成拼音
	 * 
	 * @param str
	 *            待转换的汉字字符串
	 * @return 转换成拼音码后的字符串
	 */
	public static String charToPinyin(char str, boolean isPolyphone, String separator) {
		// 创建汉语拼音处理类
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		// 输出设置，大小写，音标方式
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

		StringBuffer tempPinying = new StringBuffer();

		// 如果是中文
		if (str > 128) {
			try {
				// 转换得出结果
				String[] strs = PinyinHelper.toHanyuPinyinStringArray(str, defaultFormat);

				// 是否查出多音字，默认是查出多音字的第一个字符
				if (isPolyphone && null != separator) {
					for (int i = 0; i < strs.length; i++) {
						tempPinying.append(strs[i]);
						if (strs.length != (i + 1)) {
							// 多音字之间用特殊符号间隔起来
							tempPinying.append(separator);
						}
					}
				} else {
					tempPinying.append(strs[0]);
				}

			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
		} else {
			tempPinying.append(str);
		}

		return tempPinying.toString();

	}

	public static String hanziToPinyin(String hanzi) {
		return hanziToPinyin(hanzi, " ");
	}

	/**
	 * 将汉字转换成拼音
	 * 
	 * @param hanzi
	 * @param separator
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String hanziToPinyin(String hanzi, String separator) {
		// 创建汉语拼音处理类
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		// 输出设置，大小写，音标方式
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		// defaultFormat.setVCharType(HanyuPinyingVCharType);

		String pinyingStr = "";
		try {
			pinyingStr = PinyinHelper.toHanyuPinyinString(hanzi, defaultFormat, separator);
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		return pinyingStr;
	}

	/**
	 * 将字符串数组转换成字符串
	 * 
	 * @param str
	 * @param separator
	 *            各个字符串之间的分隔符
	 * @return
	 */
	public static String stringArrayToString(String[] str, String separator) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < str.length; i++) {
			sb.append(str[i]);
			if (str.length != (i + 1)) {
				sb.append(separator);
			}
		}
		return sb.toString();
	}

	/**
	 * 简单的将各个字符数组之间连接起来
	 * 
	 * @param str
	 * @return
	 */
	public static String stringArrayToString(String[] str) {
		return stringArrayToString(str, "");
	}

	/**
	 * 将字符数组转换成字符串
	 * 
	 * @param str
	 * @param separator
	 *            各个字符串之间的分隔符
	 * @return
	 */
	public static String charArrayToString(char[] ch, String separator) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < ch.length; i++) {
			sb.append(ch[i]);
			if (ch.length != (i + 1)) {
				sb.append(separator);
			}
		}
		return sb.toString();
	}

	/**
	 * 将字符数组转换成字符串
	 * 
	 * @param str
	 * @return
	 */
	public static String charArrayToString(char[] ch) {
		return charArrayToString(ch, " ");
	}

	/**
	 * 取汉字的首字母
	 * 
	 * @param str
	 * @param isCapital
	 *            是否是大写
	 * @return
	 */
	public static String getHeadLetter(String word, boolean isCapital) {
		if (StringUtils.isEmpty(word)) {
			return "";
		}

		char str = word.charAt(0);
		// 如果不是汉字直接返回
		if (str <= 128) {
			if (isCapital) {
				return String.valueOf(str).toUpperCase();
			}
			return String.valueOf(str);
		}
		//多音字复姓检查
		if(word.length()>1 && MULTIPLE_FIRSTNAME.containsKey(word.substring(0, 2))){
			char mulpipleChar = MULTIPLE_FIRSTNAME.get(word.substring(0, 2)).charAt(0);
			if (isCapital) {
				return String.valueOf(Character.toUpperCase(mulpipleChar));
			} else {
				return String.valueOf(mulpipleChar);
			}
		}
		//多音字姓氏
		if(word.length()>=1 &&MULTIPLE_FIRSTNAME.containsKey(word.substring(0, 1) )){
			char mulpipleChar = MULTIPLE_FIRSTNAME.get(word.substring(0, 1)).charAt(0);
			  if (isCapital) {
					return String.valueOf(Character.toUpperCase(mulpipleChar));
				} else {
					return String.valueOf(mulpipleChar);
				}
		}
		
		// 获取所有的拼音
		String[] pinyingStr = PinyinHelper.toHanyuPinyinStringArray(str);
	 
		// 截取首字符 (多音字只返回第一个，未能智能识别)
		for (String s : pinyingStr) {
			char headChar = s.charAt(0);
			// 首字母是否大写，默认是小写
			if (isCapital) {
				return String.valueOf(Character.toUpperCase(headChar));
			} else {
				return String.valueOf(headChar);
			}
		}
		return "";
	}

	/**
	 * 取汉字的首字母(默认是大写)
	 * 
	 * @param str
	 * @return
	 */
	public static String getHeadLetter(String word) {
		return getHeadLetter(word, true);
	}

	/**
	 * 查找字符串首字母
	 * 
	 * @param str
	 * @return
	 */
	public static String[] getHeadByString(String str) {
		return getHeadByString(str, true);
	}

	/**
	 * 查找字符串首字母
	 * 
	 * @param str
	 * @param isCapital
	 *            是否大写
	 * @return
	 */
	public static String[] getHeadByString(String str, boolean isCapital) {
		return getHeadByString(str, isCapital, null);
	}

	/**
	 * 查找字符串首字母
	 * 
	 * @param str
	 * @param isCapital
	 *            是否大写
	 * @param separator
	 *            分隔符
	 * @return
	 */
	public static String[] getHeadByString(String str, boolean isCapital, String separator) {
		char[] chars = str.toCharArray();
		String[] headString = new String[chars.length];
		int i = 0;
		for (char ch : chars) {

			char[] chs = getHeadByChar(ch, isCapital);
			StringBuffer sb = new StringBuffer();
			if (null != separator) {
				int j = 1;

				for (char ch1 : chs) {
					sb.append(ch1);
					if (j != chs.length) {
						sb.append(separator);
					}
					j++;
				}
			} else {
				sb.append(chs[0]);
			}
			headString[i] = sb.toString();
			i++;
		}
		return headString;
	}
	
    /**
     * 取汉字的首字母
     * @param str
     * @param isCapital 是否是大写
     * @return 
     */
    public static char[]  getHeadByChar(char str,boolean isCapital){
        //如果不是汉字直接返回
        if (str <= 128) {
            return new char[]{str};
        }
        //获取所有的拼音
        String []pinyingStr=PinyinHelper.toHanyuPinyinStringArray(str);
        //创建返回对象
        int polyphoneSize=pinyingStr.length;
        char [] headChars=new char[polyphoneSize];
        int i=0;
        //截取首字符
        for(String s:pinyingStr){
            char headChar=s.charAt(0);
            //首字母是否大写，默认是小写
            if(isCapital){
                headChars[i]=Character.toUpperCase(headChar);
             }else{
                headChars[i]=headChar;
             }
            i++;
        }
         
        return headChars;
    }

	public static void main(String[] args) {
		 System.out.println(getHeadLetter("单于s"));
		 
	}
}
